package com.cf.file.api.controller;

import com.cf.file.api.swagger.FileSwagger;
import com.cf.file.domain.FileSystem;
import com.cf.file.service.FileSystemService;
import com.cf.framework.domain.file.response.FileSystemCode;
import com.cf.framework.domain.response.CommonCode;
import com.cf.framework.domain.response.ResponseResult;
import com.cf.framework.exception.ExceptionCast;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;

@RestController
@RequestMapping("file/")
@Validated
public class FileController implements FileSwagger {
    @Reference(version = "1.0.0", retries = 0, timeout = 5000, check = false)
    private FileSystemService fileSystemService;

    @Override
    @RequestMapping(value = "upload", method = RequestMethod.POST, headers = "content-type=multipart/form-data;charset=utf-8")
    public ResponseResult upload(
            @RequestParam(value = "file", required = true)
            MultipartFile file,
            @RequestParam(value = "filetag", required = false, defaultValue = "")
            String filetag,
            @RequestParam(value = "businesskey", required = true)
            String businesskey,
            @RequestParam(value = "metadata", required = false, defaultValue = "")
            String metadata,
            @RequestParam(value = "platform", required = false, defaultValue = "ali_oss")
            String platform
    ) throws Exception {

        try {
            byte[] fileByte = file.getBytes();
            FileSystem fileSystem = fileSystemService.upload(fileByte, file.getOriginalFilename(), file.getContentType(), filetag, businesskey, metadata, platform);
            return new ResponseResult(CommonCode.SUCCESS, fileSystem);
        } catch (Exception e) {
            FileSystemCode fsDeletefileDbfail = FileSystemCode.FS_DELETEFILE_DBFAIL;
            fsDeletefileDbfail.setMessage(e.getMessage());
            ExceptionCast.cast(fsDeletefileDbfail);
            e.printStackTrace();
            return null;
        }
    }

    @Override
    @RequestMapping(value = "test", method = RequestMethod.GET)
    public ResponseResult test() throws Exception {
        String base64 = "_9j_4AAQSkZJRgABAQAAAQABAAD_2wBDAAgGBgcGBQgHBwcJCQgKDBQND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Sqc2hWqKWDy8AnBI7Y9vf9BRRWibAzhp8TSKu6QZ7gj_Cqc8SoN2STgdaKK1TJREqAu_J4HXPXr1pkTFsZ_vhfzIFFFUNCliEDdyRn9P8A4o0vXB6ZJOPyoooewI6y-hRfDqEKMgAAn6VycZLKhzgsoyRRRXNhdmaVNyIqNhbuATUi8An1OKKK6SOgYBGCAcdMiuy-HmDqkwAAwmeOM8kf0ooqKvwMa3PT7Uf6QPpn9K8V8fJv8YXhYhuQBlRwBx6UUV56-I0ex__Z";
        FileSystem upload = fileSystemService.upload(Base64.getUrlDecoder().decode(base64), "jpg", "image/jpeg", "number_plate", "carPark", "", "ali_oss");
        return new ResponseResult(CommonCode.SUCCESS, upload);
    }
}
